home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 253 / Issue 253 - March 2009 - DPCS0309DVD.ISO / Toolkit / Internet / WinHTTrack / httrack-3.43.exe / {app} / src / htslib.h < prev    next >
Encoding:
C/C++ Source or Header  |  1980-01-01  |  24.0 KB  |  667 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: Subroutines .h                                         */
  34. /* Author: Xavier Roche                                         */
  35. /* ------------------------------------------------------------ */
  36.  
  37. // Fichier librairie .h
  38.  
  39. #ifndef HTS_DEFH
  40. #define HTS_DEFH 
  41.  
  42. /* Forward definitions */
  43. #ifndef HTS_DEF_FWSTRUCT_htsrequest
  44. #define HTS_DEF_FWSTRUCT_htsrequest
  45. typedef struct htsrequest htsrequest;
  46. #endif
  47. #ifndef HTS_DEF_FWSTRUCT_htsblk
  48. #define HTS_DEF_FWSTRUCT_htsblk
  49. typedef struct htsblk htsblk;
  50. #endif
  51. #ifndef HTS_DEF_FWSTRUCT_t_dnscache
  52. #define HTS_DEF_FWSTRUCT_t_dnscache
  53. typedef struct t_dnscache t_dnscache;
  54. #endif
  55.  
  56. /* dΘfinitions globales */
  57. #include "htsglobal.h"
  58.  
  59. /* basic net definitions */
  60. #include "htsbase.h"
  61. #include "htsbasenet.h"
  62. #include "htsnet.h"
  63. #include "htsdefines.h"
  64.  
  65. /* readdir() */
  66. #ifndef _WIN32
  67. #include <sys/types.h>
  68. #include <dirent.h>
  69. #endif
  70.  
  71. /* cookies et auth */
  72. #include "htsbauth.h"
  73.  
  74. // Attention, dΘfinition existante Θgalement dans le shell
  75. // (α modifier avec celle-ci)
  76. #define POSTTOK "?>post"
  77.  
  78. #include "htsopt.h"
  79.  
  80. #define READ_ERROR (-1)
  81. #define READ_EOF (-2)
  82. #define READ_TIMEOUT (-3)
  83. #define READ_INTERNAL_ERROR (-4)
  84.  
  85. /* concat */
  86.  
  87. #if ( defined(_WIN32) && defined(_MSC_VER) && ( _MSC_VER >= 1300 ) && (_MSC_VER <= 1310 ) )
  88. /* NOTE: VC2003 inlining bug in optim mode not respecting function call sequence point */
  89. #define MSVC2003INLINEBUG __declspec(noinline)
  90. #else
  91. #define MSVC2003INLINEBUG
  92. #endif
  93. MSVC2003INLINEBUG static char* getHtsOptBuff_(httrackp *opt) {
  94.     opt->state.concat.index = ( opt->state.concat.index + 1 ) % 16;
  95.     return opt->state.concat.buff[opt->state.concat.index];
  96. }
  97. #undef MSVC2003INLINEBUG
  98. #define OPT_GET_BUFF(OPT) ( getHtsOptBuff_(OPT) )
  99.  
  100. // structure pour paramΦtres supplΘmentaires lors de la requΩte
  101. #ifndef HTS_DEF_FWSTRUCT_htsrequest_proxy
  102. #define HTS_DEF_FWSTRUCT_htsrequest_proxy
  103. typedef struct htsrequest_proxy htsrequest_proxy;
  104. #endif
  105. struct htsrequest_proxy {
  106.   int active;
  107.   char name[1024];
  108.   int port;
  109.   char bindhost[256];   // bind this host
  110. }; 
  111. #ifndef HTS_DEF_FWSTRUCT_htsrequest
  112. #define HTS_DEF_FWSTRUCT_htsrequest
  113. typedef struct htsrequest htsrequest;
  114. #endif
  115. struct htsrequest {
  116.   short int user_agent_send;  // user agent (ex: httrack/1.0 [sun])
  117.   short int http11;           // l'en tΩte peut (doit) Ωtre signΘ HTTP/1.1 et non HTTP/1.0
  118.   short int nokeepalive;      // pas de keep-alive
  119.   short int range_used;       // Range utilisΘ
  120.   short int nocompression;    // Pas de compression
  121.   short int flush_garbage;    // recycled
  122.   char user_agent[128];
  123.   char referer[256];
  124.   char from[256];
  125.   char lang_iso[64];
  126.   htsrequest_proxy proxy;              // proxy
  127. };
  128.  
  129.  
  130. // structure pour retour d'une connexion/prise d'en tΩte
  131. #ifndef HTS_DEF_FWSTRUCT_htsblk
  132. #define HTS_DEF_FWSTRUCT_htsblk
  133. typedef struct htsblk htsblk;
  134. #endif
  135. struct htsblk {
  136.   int statuscode;        // status-code, -1=erreur, 200=OK,201=..etc (cf RFC1945)
  137.   short int notmodified; // page ou fichier NON modifiΘ (transfΘrΘ)
  138.   short int is_write;    // sortie sur disque (out) ou en mΘmoire (adr)
  139.   short int is_chunk;    // mode chunk
  140.   short int compressed;  // compressΘ?
  141.   short int empty;       // vide?
  142.   short int keep_alive;  // Keep-Alive?
  143.   short int keep_alive_trailers;  // ..with trailers extension
  144.   int keep_alive_t;      // KA timeout
  145.   int keep_alive_max;    // KA number of requests
  146.   char* adr;             // adresse du bloc de mΘmoire, NULL=vide
  147.   char* headers;         // adresse des en tΩtes si prΘsents
  148.   FILE* out;             // Θcriture directe sur disque (si is_write=1)
  149.   LLint size;            // taille fichier
  150.   char msg[80];          // message Θventuel si Θchec ("\0"=non prΘcisΘ)
  151.   char contenttype[64];  // content-type ("text/html" par exemple)
  152.   char charset[64];      // charset ("iso-8859-1" par exemple)
  153.   char contentencoding[64];  // content-encoding ("gzip" par exemple)
  154.   char* location;        // on copie dedans Θventuellement la vΘritable 'location'
  155.   LLint totalsize;       // taille totale α tΘlΘcharger (-1=inconnue)
  156.   short int is_file;     // ce n'est pas une socket mais un descripteur de fichier si 1
  157.   T_SOC soc;             // ID socket
  158.   SOCaddr address;       // IP address
  159.   int     address_size;  // IP address structure length
  160.   FILE* fp;              // fichier pour file://
  161. #if HTS_USEOPENSSL
  162.   short int ssl;         // is this connection a SSL one? (https)
  163.   // BIO* ssl_soc;          // SSL structure
  164.   SSL * ssl_con;         // connection structure
  165. #endif
  166.   char lastmodified[64]; // Last-Modified
  167.   char etag[64];         // Etag
  168.   char cdispo[256];      // Content-Disposition coupΘ
  169.   LLint  crange;         // Content-Range
  170.   int debugid;           // debug connection
  171.   /* */
  172.   htsrequest req;        // paramΦtres pour la requΩte
  173.   /*char digest[32+2];   // digest md5 gΘnΘrΘ par le moteur ("" si non gΘnΘrΘ)*/
  174. };
  175.  
  176.  
  177. /* ANCIENNE STURCTURE pour cache 1.0 */
  178. #ifndef HTS_DEF_FWSTRUCT_OLD_t_proxy
  179. #define HTS_DEF_FWSTRUCT_OLD_t_proxy
  180. typedef struct OLD_t_proxy OLD_t_proxy;
  181. #endif
  182. struct OLD_t_proxy {
  183.   int active;
  184.   char name[1024];
  185.   int port;
  186. }; 
  187. #ifndef HTS_DEF_FWSTRUCT_OLD_htsblk
  188. #define HTS_DEF_FWSTRUCT_OLD_htsblk
  189. typedef struct OLD_htsblk OLD_htsblk;
  190. #endif
  191. struct OLD_htsblk {
  192.   int statuscode;  // ANCIENNE STURCTURE - status-code, -1=erreur, 200=OK,201=..etc (cf RFC1945)
  193.   int notmodified; // ANCIENNE STURCTURE - page ou fichier NON modifiΘ (transfΘrΘ)
  194.   int is_write;    // ANCIENNE STURCTURE - sortie sur disque (out) ou en mΘmoire (adr)
  195.   char* adr;       // ANCIENNE STURCTURE - adresse du bloc de mΘmoire, NULL=vide
  196.   FILE* out;       // ANCIENNE STURCTURE - Θcriture directe sur disque (si is_write=1)
  197.   int size;        // ANCIENNE STURCTURE - taille fichier
  198.   char msg[80];    // ANCIENNE STURCTURE - message Θventuel si Θchec ("\0"=non prΘcisΘ)
  199.   char contenttype[64];  // ANCIENNE STURCTURE - content-type ("text/html" par exemple)
  200.   char* location;  // ANCIENNE STURCTURE - on copie dedans Θventuellement la vΘritable 'location'
  201.   int totalsize;   // ANCIENNE STURCTURE - taille totale α tΘlΘcharger (-1=inconnue)
  202.   int is_file;     // ANCIENNE STURCTURE - ce n'est pas une socket mais un descripteur de fichier si 1
  203.   T_SOC soc;       // ANCIENNE STURCTURE - ID socket
  204.   FILE* fp;        // ANCIENNE STURCTURE - fichier pour file://
  205.   OLD_t_proxy proxy;   // ANCIENNE STURCTURE - proxy
  206.   int user_agent_send;  // ANCIENNE STURCTURE - user agent (ex: httrack/1.0 [sun])
  207.   char user_agent[64];
  208.   int http11;           // ANCIENNE STURCTURE - l'en tΩte doit Ωtre signΘ HTTP/1.1 et non HTTP/1.0
  209. };
  210. /* fin ANCIENNE STURCTURE pour cache 1.0 */
  211.  
  212. // cache pour le dns, pour Θviter de faire des gethostbyname sans arrΩt
  213. #ifndef HTS_DEF_FWSTRUCT_t_dnscache
  214. #define HTS_DEF_FWSTRUCT_t_dnscache
  215. typedef struct t_dnscache t_dnscache;
  216. #endif
  217. struct t_dnscache {
  218.   char iadr[1024];
  219.   struct t_dnscache* n;
  220.   char host_addr[HTS_MAXADDRLEN];    // 4 octets (v4), ou 16 octets (v6)
  221.   int host_length;                   // 4 normalement - ==0  alors en cours de rΘsolution
  222.                                      // ou >16 si sockaddr
  223.                                      //                 ==-1 alors erreur (host n'Θxiste pas)
  224. };
  225.  
  226.  
  227. /* Library internal definictions */
  228. #ifdef HTS_INTERNAL_BYTECODE
  229.  
  230. extern htsmutex dns_lock;
  231.  
  232. // fonctions unix/winsock
  233. int hts_read(htsblk* r,char* buff,int size);
  234. //int HTS_TOTAL_RECV_CHECK(int var);
  235. LLint check_downloadable_bytes(int rate);
  236.  
  237. #ifndef HTTRACK_DEFLIB
  238. HTSEXT_API int hts_init(void);
  239. HTSEXT_API int hts_uninit(void);
  240. HTSEXT_API int hts_uninit_module(void);
  241. HTSEXT_API int hts_resetvar(void);  /* dummy */
  242. HTSEXT_API void hts_debug(int level);
  243. HTSEXT_API httrackp* hts_create_opt(void);
  244. HTSEXT_API void hts_free_opt(httrackp *opt);
  245. HTSEXT_API void set_wrappers(httrackp *opt);    /* LEGACY */
  246. HTSEXT_API int plug_wrapper(httrackp *opt, const char *moduleName, const char* argv);
  247.  
  248. HTSEXT_API char* hts_strdup(const char* string);
  249. HTSEXT_API void* hts_malloc(size_t size);
  250. HTSEXT_API void* hts_realloc(void* data, size_t size);
  251. HTSEXT_API void hts_free(void* data);
  252. #endif
  253. extern int hts_dgb_init;
  254. extern FILE* hts_dgb_(void);
  255. #undef _
  256. #define _ ,
  257. #define HTS_DBG(FMT) do {     \
  258.   if (hts_dgb_init > 0) {     \
  259.     FILE *fp = hts_dgb_();    \
  260.     fprintf(fp, FMT);         \
  261.     fprintf(fp, "\n");        \
  262.     fflush(fp);               \
  263.   }                           \
  264. } while(0)
  265.  
  266. // fonctions principales
  267. int http_fopen(httrackp *opt,char* adr,char* fil,htsblk* retour);
  268. int http_xfopen(httrackp *opt,int mode,int treat,int waitconnect,char* xsend,char* adr,char* fil,htsblk* retour);
  269. int http_sendhead(httrackp *opt,t_cookie* cookie,int mode,char* xsend,char* adr,char* fil,char* referer_adr,char* referer_fil,htsblk* retour);
  270. htsblk httpget(httrackp *opt,char* url);
  271. //int newhttp(char* iadr,char* err=NULL);
  272. int newhttp(httrackp *opt,const char* iadr,htsblk* retour,int port,int waitconnect);
  273. HTS_INLINE void deletehttp(htsblk* r);
  274. HTS_INLINE int  deleteaddr(htsblk* r);
  275. HTS_INLINE void deletesoc(T_SOC soc);
  276. HTS_INLINE void deletesoc_r(htsblk* r);
  277. htsblk http_location(httrackp *opt,char* adr,char* fil,char* loc);
  278. htsblk http_test(httrackp *opt,char* adr,char* fil,char* loc);
  279. int check_readinput(htsblk* r);
  280. int check_readinput_t(T_SOC soc, int timeout);
  281. void http_fread(T_SOC soc,htsblk* retour);
  282. LLint http_fread1(htsblk* r);
  283. void treathead(t_cookie* cookie,char* adr,char* fil,htsblk* retour,char* rcvd);
  284. void treatfirstline(htsblk* retour,char* rcvd);
  285. #ifndef HTTRACK_DEFLIB
  286. HTSEXT_API void infostatuscode(char* msg,int statuscode);
  287. #endif
  288.  
  289. // sous-fonctions
  290. htsblk xhttpget(httrackp *opt,char* adr,char* fil);
  291. htsblk http_gethead(httrackp *opt,char* adr,char* fil);
  292. LLint http_xfread1(htsblk* r,int bufl);
  293. HTS_INLINE t_hostent* hts_gethostbyname(httrackp *opt,const char* iadr, void* v_buffer);
  294. #ifndef HTTRACK_DEFLIB
  295. HTSEXT_API t_hostent* vxgethostbyname(char* hostname, void* v_buffer);
  296. #endif
  297. t_hostent* _hts_ghbn(t_dnscache* cache,const char* iadr,t_hostent* retour);
  298. int ftp_available(void);
  299. #if HTS_DNSCACHE
  300. void hts_cache_free(t_dnscache* cache);
  301. int hts_dnstest(httrackp *opt, const char* _iadr);
  302. t_dnscache* _hts_cache(httrackp *opt);
  303. #endif
  304.  
  305. // outils divers
  306. HTS_INLINE TStamp time_local(void);
  307. #ifndef HTTRACK_DEFLIB
  308. HTSEXT_API HTS_INLINE TStamp mtime_local(void);
  309. #endif
  310. void sec2str(char *s,TStamp t);
  311. #ifndef HTTRACK_DEFLIB
  312. HTSEXT_API void qsec2str(char *st,TStamp t);
  313. #endif
  314. void time_gmt_rfc822(char* s);
  315. void time_local_rfc822(char* s);
  316. struct tm* convert_time_rfc822(struct tm* buffer, const char* s);
  317. int set_filetime(const char* file,struct tm* tm_time);
  318. int set_filetime_rfc822(const char* file,const char* date);
  319. int get_filetime_rfc822(const char* file,char* date);
  320. HTS_INLINE void time_rfc822(char* s,struct tm * A);
  321. HTS_INLINE void time_rfc822_local(char* s,struct tm * A);
  322. #ifndef HTTRACK_DEFLIB
  323. HTSEXT_API char* int2char(strc_int2bytes2* strc, int n);
  324. HTSEXT_API char* int2bytes(strc_int2bytes2* strc, LLint n);
  325. HTSEXT_API char* int2bytessec(strc_int2bytes2* strc, long int n);
  326. HTSEXT_API char** int2bytes2(strc_int2bytes2* strc, LLint n);
  327. #endif
  328. HTS_INLINE int sendc(htsblk* r, const char* s);
  329. int finput(int fd,char* s,int max);
  330. int binput(char* buff,char* s,int max);
  331. int linput(FILE* fp,char* s,int max);
  332. int linputsoc(T_SOC soc, char* s, int max);
  333. int linputsoc_t(T_SOC soc, char* s, int max, int timeout);
  334. int linput_trim(FILE* fp,char* s,int max);
  335. int linput_cpp(FILE* fp,char* s,int max);
  336. void rawlinput(FILE* fp,char* s,int max);
  337. char* strstrcase(char *s,char *o);
  338. int ident_url_absolute(const char* url,char* adr,char* fil);
  339. void fil_simplifie(char* f);
  340. int is_unicode_utf8(unsigned char* buffer, unsigned int size);
  341. void map_characters(unsigned char* buffer, unsigned int size, unsigned int* map);
  342. int ishtml(httrackp *opt,const char* urlfil);
  343. int ishtml_ext(const char* a);
  344. int ishttperror(int err);
  345. void guess_httptype(httrackp *opt,char *s,const char *fil);
  346. #ifndef HTTRACK_DEFLIB
  347. HTSEXT_API void get_httptype(httrackp *opt,char *s,const char *fil,int flag);
  348. #endif
  349. int get_userhttptype(httrackp *opt,char *s,const char *fil);
  350. void give_mimext(char *s,const char *st);
  351. #ifndef HTTRACK_DEFLIB
  352. HTSEXT_API int is_knowntype(httrackp *opt,const char *fil);
  353. HTSEXT_API int is_userknowntype(httrackp *opt,const char *fil);
  354. HTSEXT_API int is_dyntype(const char *fil);
  355. HTSEXT_API char* get_ext(char *catbuff, const char *fil);
  356. #endif
  357. int may_unknown(httrackp *opt,const char* st);
  358. int may_bogus_multiple(httrackp *opt, const char* mime, const char *filename);
  359. int may_unknown2(httrackp *opt,const char* mime, const char *filename);
  360. #ifndef HTTRACK_DEFLIB
  361. HTSEXT_API char* jump_identification(const char*);
  362. HTSEXT_API char* jump_normalized(const char*);
  363. HTSEXT_API char* jump_toport(const char*);
  364. HTSEXT_API char* fil_normalized(const char* source, char* dest);
  365. HTSEXT_API char* adr_normalized(const char* source, char* dest);
  366. #endif
  367. char* strrchr_limit(const char* s, char c, const char* limit);
  368. char* strstr_limit(const char* s, const char* sub, const char* limit);
  369. HTS_INLINE char* jump_protocol(const char* source);
  370. void code64(unsigned char* a,int size_a,unsigned char* b,int crlf);
  371. #ifndef HTTRACK_DEFLIB
  372. HTSEXT_API void unescape_amp(char* s);
  373. HTSEXT_API void escape_spc_url(char* s);
  374. HTSEXT_API void escape_in_url(char* s);
  375. HTSEXT_API void escape_uri(char* s);
  376. HTSEXT_API void escape_uri_utf(char* s);
  377. HTSEXT_API void escape_check_url(char* s);
  378. HTSEXT_API char* escape_check_url_addr(char *catbuff, const char* s);
  379. HTSEXT_API void x_escape_http(char* s,int mode);
  380. HTSEXT_API void x_escape_html(char* s);
  381. HTSEXT_API void escape_remove_control(char* s);
  382. HTSEXT_API void escape_for_html_print(char* s, char* d);
  383. HTSEXT_API void escape_for_html_print_full(char* s, char* d);
  384. #endif
  385. #ifndef HTTRACK_DEFLIB
  386. HTSEXT_API char* unescape_http(char *catbuff, const char* s);
  387. HTSEXT_API char* unescape_http_unharm(char *catbuff, const char* s, int no_high);
  388. HTSEXT_API char* antislash_unescaped(char *catbuff, const char* s);
  389. HTSEXT_API char* concat(char *catbuff,const char* a,const char* b);
  390. HTSEXT_API char* fconcat(char *catbuff, const char* a, const char* b);
  391. HTSEXT_API char* fconv(char *catbuff, const char* a);
  392. #endif
  393. #define copychar(catbuff,a) concat(catbuff,(a),NULL)
  394. char* fslash(char *catbuff, const char* a);
  395. #ifndef HTTRACK_DEFLIB
  396. HTSEXT_API int hts_log(httrackp *opt, const char* prefix, const char *msg);
  397. #endif
  398.  
  399. char* convtolower(char *catbuff, const char* a);
  400. void hts_lowcase(char* s);
  401. void hts_replace(char *s,char from,char to);
  402. int multipleStringMatch(const char *s, const char *match);
  403.  
  404.  
  405. void fprintfio(FILE* fp,char* buff,char* prefix);
  406.  
  407. #ifdef _WIN32
  408. #else
  409. int sig_ignore_flag( int setflag );     // flag ignore
  410. #endif
  411.  
  412. void cut_path(char* fullpath,char* path,char* pname);
  413. int fexist(const char* s);
  414. /*LLint fsize(const char* s);    */
  415. off_t fpsize(FILE* fp);
  416. off_t fsize(const char* s);    
  417. /* root dir */
  418. #ifndef HTTRACK_DEFLIB
  419. HTSEXT_API char* hts_rootdir(char* file);
  420. #endif
  421.  
  422. // Threads
  423. typedef void* ( *beginthread_type )( void * );
  424. /*unsigned long _beginthread( beginthread_type start_address, unsigned stack_size, void *arglist );*/
  425.  
  426. /* variables globales */
  427. extern HTSEXT_API hts_stat_struct HTS_STAT;
  428. extern int _DEBUG_HEAD;
  429. extern FILE* ioinfo;
  430.  
  431. /* constantes */
  432. extern const char* hts_mime_keep[];
  433. extern const char* hts_mime[][2];
  434. extern const char* hts_main_mime[];
  435. extern const char* hts_detect[];
  436. extern const char* hts_detectbeg[];
  437. extern const char* hts_nodetect[];
  438. extern const char* hts_detectURL[];
  439. extern const char* hts_detectandleave[];
  440. extern const char* hts_detect_js[];
  441.  
  442. // htsmodule.c definitions
  443. extern void* openFunctionLib(const char* file_);
  444. extern void* getFunctionPtr(void* handle, const char* fncname);
  445. extern void  closeFunctionLib(void* handle);
  446.  
  447. extern void clearCallbacks(htscallbacks* chain);
  448. extern size_t hts_get_callback_offs(const char *name);
  449. int hts_set_callback(t_hts_htmlcheck_callbacks *callbacks, const char *name, void *function);
  450. void *hts_get_callback(t_hts_htmlcheck_callbacks *callbacks, const char *name);
  451.  
  452. #define CBSTRUCT(OPT) ((t_hts_htmlcheck_callbacks*) ((OPT)->callbacks_fun))
  453. #define GET_USERCALLBACK(OPT, NAME) ( CBSTRUCT(OPT)-> NAME .fun )
  454. #define GET_USERARG(OPT, NAME) ( CBSTRUCT(OPT)-> NAME .carg )
  455. #define GET_USERDEF(OPT, NAME) ( \
  456.   (CBSTRUCT(OPT) != NULL && CBSTRUCT(OPT)-> NAME .fun != NULL) \
  457.     ? ( GET_USERARG(OPT, NAME) ) \
  458.     : ( default_callbacks. NAME .carg ) \
  459. )
  460. #define GET_CALLBACK(OPT, NAME) ( \
  461.   (CBSTRUCT(OPT) != NULL && CBSTRUCT(OPT)-> NAME .fun != NULL) \
  462.     ? ( GET_USERCALLBACK(OPT, NAME ) ) \
  463.   : ( default_callbacks. NAME .fun ) \
  464. )
  465.  
  466. /* Predefined macros */
  467. #define RUN_CALLBACK_NOARG(OPT, NAME) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME))
  468. #define RUN_CALLBACK0(OPT, NAME) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT)
  469. #define RUN_CALLBACK1(OPT, NAME, ARG1) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT, ARG1)
  470. #define RUN_CALLBACK2(OPT, NAME, ARG1, ARG2) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT, ARG1, ARG2)
  471. #define RUN_CALLBACK3(OPT, NAME, ARG1, ARG2, ARG3) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT, ARG1, ARG2, ARG3)
  472. #define RUN_CALLBACK4(OPT, NAME, ARG1, ARG2, ARG3, ARG4) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT, ARG1, ARG2, ARG3, ARG4)
  473. #define RUN_CALLBACK5(OPT, NAME, ARG1, ARG2, ARG3, ARG4, ARG5) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT, ARG1, ARG2, ARG3, ARG4, ARG5)
  474. #define RUN_CALLBACK6(OPT, NAME, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6)
  475. #define RUN_CALLBACK7(OPT, NAME, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7)
  476. #define RUN_CALLBACK8(OPT, NAME, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8) GET_CALLBACK(OPT, NAME)(GET_USERARG(OPT, NAME), OPT, ARG1, ARG2, ARG3, ARG4, ARG5, ARG6, ARG7, ARG8)
  477.  
  478. /*
  479. #define GET_CALLBACK(OPT, NAME, ARG) ( \
  480.   ( \
  481.     ( ARG ) = GET_USERDEF(OPT, NAME), \
  482.     ( \
  483.          (CBSTRUCT(OPT) != NULL && CBSTRUCT(OPT)-> NAME .fun != NULL) \
  484.         ? ( GET_USERCALLBACK(OPT, NAME ) ) \
  485.       : ( default_callbacks. NAME .fun ) \
  486.     ) \
  487.   ) \
  488. )
  489. */
  490.  
  491. #endif    // internals
  492.  
  493. #undef PATH_SEPARATOR
  494. #ifdef _WIN32
  495. #define PATH_SEPARATOR '\\'
  496. #else
  497. #define PATH_SEPARATOR '/'
  498. #endif
  499.  
  500. /* Spaces: CR,LF,TAB,FF */
  501. #define  is_space(c)      ( ((c)==' ') || ((c)=='\"') || ((c)==10) || ((c)==13) || ((c)==9) || ((c)==12) || ((c)==11) || ((c)=='\'') )
  502. #define  is_realspace(c)  ( ((c)==' ')                || ((c)==10) || ((c)==13) || ((c)==9) || ((c)==12) || ((c)==11)                )
  503. #define  is_taborspace(c) ( ((c)==' ')                                          || ((c)==9)                             )
  504. #define  is_quote(c)      (               ((c)=='\"')                                                    || ((c)=='\'') )
  505. #define  is_retorsep(c)   (                              ((c)==10) || ((c)==13) || ((c)==9)                                          )
  506. //HTS_INLINE int is_space(char);
  507. //HTS_INLINE int is_realspace(char);
  508.  
  509. #define HTTP_IS_REDIRECT(code) ( \
  510.      (code) == 301               \
  511.   || (code) == 302               \
  512.   || (code) == 303               \
  513.   || (code) == 307               \
  514.   )
  515. #define HTTP_IS_NOTMODIFIED(code) ( \
  516.      (code) == 304               \
  517.   )
  518. #define HTTP_IS_OK(code) ( ( (code) / 100 ) == 2 )
  519. #define HTTP_IS_ERROR(code) ( !HTTP_IS_OK(code) && !HTTP_IS_REDIRECT(code) && !HTTP_IS_NOTMODIFIED(code) )
  520.  
  521. // compare le dΘbut de f avec s et retourne la position de la fin
  522. // 'A=a' (case insensitive)
  523. HTS_STATIC int strfield(const char* f,const char* s) {
  524.   int r=0;
  525.   while (streql(*f,*s) && ((*f)!=0) && ((*s)!=0)) { f++; s++; r++; }
  526.   if (*s==0)
  527.     return r;
  528.   else
  529.     return 0;
  530. }
  531. HTS_STATIC int strcmpnocase(char* a,char* b) {
  532.   while(*a) {
  533.     int cmp = hichar(*a) - hichar(*b);
  534.     if (cmp != 0)
  535.       return cmp;
  536.     a++;
  537.     b++;
  538.   }
  539.   return 0;
  540. }
  541.  
  542. #ifdef _WIN32
  543. #define strcasecmp(a,b) stricmp(a,b)
  544. #define strncasecmp(a,b,n) strnicmp(a,b,n)
  545. #endif
  546.  
  547. #define strfield2(f,s) ( (strlen(f)!=strlen(s)) ? 0 : (strfield(f,s)) )
  548.  
  549. // is this MIME an hypertext MIME (text/html), html/js-style or other script/text type?
  550. #define HTS_HYPERTEXT_DEFAULT_MIME "text/html"
  551.  
  552. #if HTS_USEMMS
  553. #define OPT_MMS(a) (strfield2((a), "video/x-ms-asf") != 0)
  554. #else
  555. #define OPT_MMS(a) (0)
  556. #endif
  557. #define is_hypertext_mime__(a) \
  558.   ( (strfield2((a),"text/html")!=0)\
  559.   || (strfield2((a),"application/x-javascript")!=0) \
  560.   || (strfield2((a),"text/css")!=0) \
  561.   /*|| (strfield2((a),"text/vnd.wap.wml")!=0)*/ \
  562.   || (strfield2((a),"image/svg+xml")!=0) \
  563.   || (strfield2((a),"image/svg-xml")!=0) \
  564.   /*|| (strfield2((a),"audio/x-pn-realaudio")!=0) */\
  565.   || (strfield2((a),"application/x-authorware-map")!=0) \
  566.   )
  567. #define may_be_hypertext_mime__(a) \
  568.    (\
  569.      (strfield2((a),"audio/x-pn-realaudio")!=0) \
  570.      || (strfield2((a),"audio/x-mpegurl")!=0) \
  571.      /*|| (strfield2((a),"text/xml")!=0) || (strfield2((a),"application/xml")!=0) : TODO: content check */ \
  572.      || OPT_MMS(a) \
  573.   )
  574.  
  575.  
  576. /* Library internal definictions */
  577. #ifdef HTS_INTERNAL_BYTECODE
  578.  
  579. // check if (mime, file) is hypertext
  580. HTS_STATIC int is_hypertext_mime(httrackp *opt,const char* mime, const char* file) {
  581.   if (is_hypertext_mime__(mime))
  582.     return 1;
  583.   if (may_unknown(opt,mime)) {
  584.     char guessed[256];
  585.     guessed[0] = '\0';
  586.     guess_httptype(opt,guessed, file);
  587.     return is_hypertext_mime__(guessed);
  588.   }
  589.   return 0;
  590. }
  591.  
  592. // check if (mime, file) might be "false" hypertext
  593. HTS_STATIC int may_be_hypertext_mime(httrackp *opt,const char* mime, const char* file) {
  594.   if (may_be_hypertext_mime__(mime))
  595.     return 1;
  596.   if (file != NULL && file[0] != '\0' && may_unknown(opt,mime)) {
  597.     char guessed[256];
  598.     guessed[0] = '\0';
  599.     guess_httptype(opt,guessed, file);
  600.     return may_be_hypertext_mime__(guessed);
  601.   }
  602.   return 0;
  603. }
  604.  
  605. // compare (mime, file) with reference
  606. HTS_STATIC int compare_mime(httrackp *opt,const char* mime, const char* file, const char* reference) {
  607.   if (is_hypertext_mime__(mime) || may_be_hypertext_mime__(mime))
  608.     return strfield2(mime, reference);
  609.   if (file != NULL && file[0] != '\0' && may_unknown(opt,mime)) {
  610.     char guessed[256];
  611.     guessed[0] = '\0';
  612.     guess_httptype(opt,guessed, file);
  613.     return strfield2(guessed, reference);
  614.   }
  615.   return 0;
  616. }
  617.  
  618. #endif
  619.  
  620. #ifdef _WIN32_WCE_XXC
  621. extern char cwd[MAX_PATH+1];
  622. HTS_STATIC char *getcwd_ce(char *buffer, int maxlen) {
  623.     TCHAR fileUnc[MAX_PATH+1];
  624.     char* plast;
  625.     
  626.     if(cwd[0] == 0)
  627.     {
  628.         GetModuleFileName(NULL, fileUnc, MAX_PATH);
  629.         WideCharToMultiByte(CP_ACP, 0, fileUnc, -1, cwd, MAX_PATH, NULL, NULL);
  630.         plast = strrchr(cwd, '\\');
  631.         if(plast)
  632.             *plast = 0;
  633.         /* Special trick to keep start menu clean... */
  634.         if(_stricmp(cwd, "\\windows\\start menu") == 0)
  635.             strcpy(cwd, "\\Apps");
  636.     }
  637.     if(buffer)
  638.         strncpy(buffer, cwd, maxlen);
  639.     return cwd;
  640. }
  641. #undef getcwd
  642. #define getcwd getcwd_ce
  643. #endif
  644.  
  645. /* dirent() compatibility */
  646. #ifdef _WIN32
  647. #define HTS_DIRENT_SIZE 256
  648. struct dirent {
  649.   ino_t          d_ino;       /* ignored */
  650.   off_t          d_off;       /* ignored */
  651.   unsigned short d_reclen;    /* ignored */
  652.   unsigned char  d_type;      /* ignored */
  653.   char           d_name[HTS_DIRENT_SIZE]; /* filename */
  654. };
  655. typedef struct DIR DIR;
  656. struct DIR {
  657.   HANDLE h;
  658.   struct dirent entry;
  659.   char *name;
  660. };
  661. DIR *opendir(const char *name);
  662. struct dirent *readdir(DIR *dir);
  663. int closedir(DIR *dir);
  664. #endif
  665.  
  666. #endif
  667.